#include <stdio.h>
#include <glob.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

#define DELIMS " \t\n"  //规定分隔符

static void prompt(void)
{
	printf("mysh-0.1$");
}

struct cmd_st
{
	glob_t globres;
};

static void parse(char *line,struct cmd_st *res)
{
	char *tok;
	glob_t globres;
	
	int i = 0;
	while(1)
	{
		tok = strsep(&line,DELIMS);
		if(tok == NULL)
			break;
		if(tok[0] == '\0')
			continue;
		
		glob(tok,GLOB_NOCHECK|GLOB_APPEND*i,NULL,&res->globres);
		i = 1;
	}
}

int main(void)
{
	char *linebuf = NULL;
	size_t linebuf_size = 0;
	struct cmd_st cmd;
	pid_t pid;	
	
	while(1)
	{
		prompt();
		
		if(getline(&linebuf,&linebuf_size,stdin) < 0)
			break;
		
		parse(linebuf,&cmd);
		
		if(0)  //是内部命令
		{}
		
		else
		{
			pid = fork();
			if(pid < 0)
			{
				perror("fork()");
				exit(1);
			}
			
			if(pid == 0) 	//child
			{
				execvp(cmd.globres.gl_pathv[0],cmd.globres.gl_pathv);
				perror("execvp()");
				exit(1);
			}
			
			else
			{
				wait(NULL);
			}
		}
		
		
	}
	
	exit(0);
}
